#!/bin/bash

#Created by turk 2017-01-11
#Version 1.0 



if [ $# -lt 1 ]; then
	echo `date +"%Y-%m-%d %H:%M:%S"`  "Usage:<DATE1 YYYYMMDD> <DATA2 YYYY-MM-DD>"
	exit 1
fi

DATE1=$1
DATE2=$2

WORKPATH=/home/hadoop/work/
JAVALIB=${WORKPATH}/lib/
LOGFILE=${WORKPATH}/log/metis_app_stat.log
LOGPATH=${WORKPATH}/log/

#--ORACLE--
IP=192.168.5.25
USERNAME=metis
PASSWORD=h16aug8v3w


echo `date +"%Y-%m-%d %H:%M:%S"`  "-----------------------------" | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "Shell Version 1.0 (2017-01-11)"     | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "METIS APP STAT"  | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "-----------------------------"  | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "Start..."  | tee -a $LOGFILE



#udfstr="add jar hdfs://nameservice1/hive/udf/ipconvert.jar;\
#CREATE TEMPORARY FUNCTION IpToString AS 'com.aotain.common.IpToString';\
#CREATE TEMPORARY FUNCTION IpToLong AS 'com.aotain.common.IpToLong';"

#--------------vio_app_stat_ip_d----------------------
sql="insert overwrite directory '/tmp/tmp_flow_app_stat_d' \
 select '$DATE2'  reporttime,d.appid,d.apptype,d.appname,d.apptypename,\
 sum(upstreamoctets) upstreamoctets,sum(upstreampacket) upstreampacket,sum(dnstreamoctets) dnstreamoctets,sum(dnstreampacket) dnstreampacket \
 from (select dt,destinationip,destinationport,packettype,packetsubtype,protocolid,upstreamoctets,upstreampacket,dnstreamoctets,dnstreampacket from ods_udii_nohttp union all \
select dt,destinationip,destinationport,packettype,packetsubtype,protocolid,upstreamoctets,upstreampacket,dnstreamoctets,dnstreampacket from ods_udii_http) a \
 join vio.vio_cfg_apptype d on d.apptype = a.packettype and d.appid = a.packetsubtype \
 where dt = $DATE1 and d.appid>0 and d.apptype>0 \
 group by d.appid,d.apptype,d.appname,d.apptypename;"
echo $sql > $LOGFILE
hive -e "$udfstr$sql" >> $LOGFILE 2>&1

#-----------------SQOOP----------------------------
export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$JAVALIB/hades-project-0.0.1-SNAPSHOT.jar

sqoop export --connect jdbc:oracle:thin:@$IP:1521:BSMP \
--username $USERNAME --password=$PASSWORD \
--table METIS_APP_STAT_IP_D  \
--columns REPORTTIME,APPID,APPTYPE,APPNAME,APPTYPENAME,UPSTREAMOCTETS,UPSTREAMPACKET,DNSTREAMOCTETS,DNSTREAMPACKET \
--export-dir /tmp/tmp_flow_app_stat_d \
--fields-terminated-by '\001' \
-m 2 >> $LOGFILE 2>&1

echo `date +"%Y-%m-%d %H:%M:%S"`      "Quit" | tee -a $LOGFILE

exit 0
